/**
 * @param {number[][]} intervals
 * @return {number}
 */
// 差分数组的原理
var minGroups = function (intervals) {
  let max = intervals[0][1];
  for (let interval of intervals) {
    max = Math.max(max, interval[1]);
  }
  const diff = new Array(max + 1).fill(0);
  for (let interval of intervals) {
    diff[interval[0]] += 1;
    if (interval[1] + 1 <= max) {
      diff[interval[1] + 1] -= 1;
    }
  }
  let t = 0,
    ans = 0;
  for (let i = 1; i <= max; i++) {
    t = t + diff[i];
    ans = Math.max(ans, t);
  }
  return ans;
};
let intervals = [
  [5, 10],
  [6, 8],
  [1, 5],
  [2, 3],
  [1, 10],
];
console.log(minGroups(intervals));
intervals = [
  [1, 3],
  [5, 6],
  [8, 10],
  [11, 13],
];
console.log(minGroups(intervals));
